package com.hxkcloud.patient.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.activitys.CertificationActivity;
import com.hxkcloud.patient.activitys.MessageMainActivity;
import com.hxkcloud.patient.activitys.PersonalInfoActivity;
import com.hxkcloud.patient.activitys.ScanCodeActivity;
import com.hxkcloud.patient.activitys.SettingActivity;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.views.CircleImageView;

/**
 * Created by william_sun on 2016-07-06.
 * Description:我的模块(nurse),医生也用这个
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    private View view;
    private LinearLayout ll_title;
    private ImageView iv_search;
    private TextView tv_title;
    private ImageView iv_message;
    private ImageView iv_msgpoint;

    private CircleImageView userIconCI;
    private TextView userNameTV;
    private TextView profileTV;

    private LinearLayout personalInfoLayout;
    private LinearLayout erweimaLayout;
    private LinearLayout renzhengLayout;
    private LinearLayout kefuLayout;
    private LinearLayout settingLayout;

    public void showPoint(boolean show) {
        if (iv_msgpoint != null)
            if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_doctor)) {
                iv_msgpoint.setVisibility(View.GONE);
            } else {
                if (YjhApplication.msg_new) {
                    iv_msgpoint.setVisibility(View.VISIBLE);
                } else {
                    iv_msgpoint.setVisibility(View.INVISIBLE);
                }
            }
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    public void gotoRZ() {
        Intent certIntent = new Intent(getActivity(), CertificationActivity.class);
        startActivity(certIntent);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView();
        initData();
        initListener();
        return view;
    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_search = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);
        iv_message = (ImageView) view.findViewById(R.id.iv_title_func);
        iv_msgpoint = (ImageView) view.findViewById(R.id.iv_title_point);

        setTitlePadding(ll_title);
        iv_search.setVisibility(View.INVISIBLE);
        tv_title.setText(getResources().getText(R.string.mine_title_text));
        iv_message.setVisibility(View.VISIBLE);
        iv_message.setImageResource(R.mipmap.nav_message);
        iv_search.setOnClickListener(this);
        iv_message.setOnClickListener(this);

        userIconCI = (CircleImageView) view.findViewById(R.id.mine_ci_usericon);
        userNameTV = (TextView) view.findViewById(R.id.mine_tv_username);
        profileTV = (TextView) view.findViewById(R.id.mine_tv_profile);

        personalInfoLayout = (LinearLayout) view.findViewById(R.id.mine_layout_pernalinfo);
        erweimaLayout = (LinearLayout) view.findViewById(R.id.mine_layout_erweima);
        renzhengLayout = (LinearLayout) view.findViewById(R.id.mine_layout_renzheng);
//        kefuLayout = (LinearLayout) view.findViewById(R.id.mine_layout_kefu);
        settingLayout = (LinearLayout) view.findViewById(R.id.mine_layout_setting);

        if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_doctor)) {
            iv_msgpoint.setVisibility(View.GONE);
            iv_message.setVisibility(View.GONE);
        }

    }

    @Override
    protected void initListener() {
        iv_message.setOnClickListener(this);

        personalInfoLayout.setOnClickListener(this);
        erweimaLayout.setOnClickListener(this);
        renzhengLayout.setOnClickListener(this);
//        kefuLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        userNameTV.setText(loginInfo.getName());
        profileTV.setText(loginInfo.getSpecialty());
        if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_doctor)) {
            if (loginInfo.getSex().equals("1")) {
                imageLoader.displayImage(loginInfo.getHeadImg(), userIconCI, ImageOptionUtils.getInstance().getDoctorHeadFemaleOptions());
            } else {
                imageLoader.displayImage(loginInfo.getHeadImg(), userIconCI, ImageOptionUtils.getInstance().getDoctorHeadMaleOptions());
            }
        } else {
            imageLoader.displayImage(loginInfo.getHeadImg(), userIconCI, ImageOptionUtils.getInstance().getNurseHeadOptions());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();

        if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_doctor)) {
            iv_msgpoint.setVisibility(View.GONE);
        } else {
            if (YjhApplication.msg_new) {
                iv_msgpoint.setVisibility(View.VISIBLE);
            } else {
                iv_msgpoint.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_title_func://消息
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    Intent iM = new Intent(mContext, MessageMainActivity.class);
                    startActivity(iM);
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.mine_layout_pernalinfo:
                Intent pernalinfoIntent = new Intent(getActivity(), PersonalInfoActivity.class);
                startActivity(pernalinfoIntent);
                break;
            case R.id.mine_layout_erweima://二维码
                Intent scancodeIntent = new Intent(getActivity(), ScanCodeActivity.class);
                startActivity(scancodeIntent);
                break;
            case R.id.mine_layout_renzheng://认证
                Intent certIntent = new Intent(getActivity(), CertificationActivity.class);
                startActivity(certIntent);
                break;
//            case R.id.mine_layout_kefu://在线客服
//
//                break;
            case R.id.mine_layout_setting://设置
                Intent settingIntent = new Intent(getActivity(), SettingActivity.class);
                startActivity(settingIntent);
                break;


        }


    }
}
